test(cloud-tasks/tutorial-gcf/function): mock sendgrid package to prevent 401 errors in CI pipeline#4325
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the test suite for the Cloud Tasks tutorial function by replacing the request-promise mock with a mock for @sendgrid/mail. The reviewer suggested two valuable improvements: first, since the SendGrid API is now mocked, the tests can be made fully hermetic and offline-friendly by replacing the Secret Manager API call in the before hook with a dummy API key; second, the newly exposed sendGridStub should be utilized in assertions to verify that the email payload is constructed and sent correctly.
amcolin
left a comment
There was a problem hiding this comment.
Changes look solid to me 👍 . Great refactor! Moving away from fetching live secrets via Secret Manager.
iennae
left a comment
There was a problem hiding this comment.
Do we still need this sample?
iennae
left a comment
There was a problem hiding this comment.
might want to add a comment inline so that someone doesn't need to look back at this PR to know why secretmanager isn't there so they make a similar decision in the future.
Description
This PR fixes a CI pipeline failure in the
cloud-tasks/tutorial-gcf/functiontest suite.The test
send succeedswas previously failing with a401 Unauthorizederror (API key does not start with "SG.") because the@sendgrid/mailpackage was making actual HTTP requests to the SendGrid API during the test execution. Since the CI environment lacks a valid production API key, the SDK rejected the request.Changes
@sendgrid/mailpackage usingproxyquireandsinon.stubinsidegetSample()to isolate the tests from external network calls.beforehook, replacing it with a local dummy key (SG.dummy_key_for_testing) to make the test suite 100% hermetic and offline-capable.sinon.assert.calledOnceWithExactlyin thesend succeedstest case to verify that the correct recipient, sender, subject, and template structure are passed to SendGrid.401validation error by bypassing the live SDK initialization during test runs, ensuring the test suite resolves locally with a mocked200status code.Fixes Internal: b/516872192
Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.
Checklist
npm test(see Testing)npm run lint(see Style)GoogleCloudPlatform/nodejs-docs-samples. Not a fork.